import { Router } from 'express';
import knowledgeController from '@controllers/knowledge.controller';
import { authenticate } from '@middleware/auth';

const router = Router();

/**
 * Knowledge Base Routes
 */

/**
 * @swagger
 * /knowledge:
 *   get:
 *     summary: Get knowledge base documents
 *     tags: [Knowledge Base]
 *     parameters:
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: Full-text search query
 *       - in: query
 *         name: category
 *         schema:
 *           type: string
 *         description: Filter by category
 *       - in: query
 *         name: tags
 *         schema:
 *           type: string
 *         description: Filter by tags (comma-separated)
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [draft, published, archived]
 *         description: Filter by status
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 20
 *     responses:
 *       200:
 *         description: Success
 */
router.get('/', knowledgeController.getKnowledgeDocs);

/**
 * @swagger
 * /knowledge/categories:
 *   get:
 *     summary: Get all categories
 *     tags: [Knowledge Base]
 *     responses:
 *       200:
 *         description: List of categories
 */
router.get('/categories', knowledgeController.getCategories);

/**
 * @swagger
 * /knowledge/popular:
 *   get:
 *     summary: Get popular knowledge documents
 *     tags: [Knowledge Base]
 *     parameters:
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *     responses:
 *       200:
 *         description: Popular documents
 */
router.get('/popular', knowledgeController.getPopularDocs);

/**
 * @swagger
 * /knowledge/{id}:
 *   get:
 *     summary: Get knowledge document by ID
 *     tags: [Knowledge Base]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Document details
 *       404:
 *         description: Document not found
 */
router.get('/:id', knowledgeController.getKnowledgeDoc);

/**
 * @swagger
 * /knowledge:
 *   post:
 *     summary: Create new knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - content
 *               - category
 *             properties:
 *               title:
 *                 type: string
 *               content:
 *                 type: string
 *               summary:
 *                 type: string
 *               category:
 *                 type: string
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *               status:
 *                 type: string
 *                 enum: [draft, published]
 *     responses:
 *       201:
 *         description: Document created
 *       401:
 *         description: Unauthorized
 */
router.post('/', authenticate, knowledgeController.createKnowledgeDoc);

/**
 * @swagger
 * /knowledge/{id}:
 *   put:
 *     summary: Update knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *     responses:
 *       200:
 *         description: Document updated
 *       401:
 *         description: Unauthorized
 *       404:
 *         description: Document not found
 */
router.put('/:id', authenticate, knowledgeController.updateKnowledgeDoc);

/**
 * @swagger
 * /knowledge/{id}:
 *   delete:
 *     summary: Delete knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Document deleted
 *       401:
 *         description: Unauthorized
 *       404:
 *         description: Document not found
 */
router.delete('/:id', authenticate, knowledgeController.deleteKnowledgeDoc);

/**
 * @swagger
 * /knowledge/{id}/publish:
 *   post:
 *     summary: Publish knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Document published
 */
router.post('/:id/publish', authenticate, knowledgeController.publishKnowledgeDoc);

/**
 * @swagger
 * /knowledge/{id}/archive:
 *   post:
 *     summary: Archive knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Document archived
 */
router.post('/:id/archive', authenticate, knowledgeController.archiveKnowledgeDoc);

/**
 * @swagger
 * /knowledge/{id}/rating:
 *   post:
 *     summary: Add rating to knowledge document
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - rating
 *             properties:
 *               rating:
 *                 type: number
 *                 minimum: 1
 *                 maximum: 5
 *               comment:
 *                 type: string
 *     responses:
 *       200:
 *         description: Rating added
 */
router.post('/:id/rating', authenticate, knowledgeController.addRating);

/**
 * @swagger
 * /knowledge/ai/ask:
 *   post:
 *     summary: Ask AI question about knowledge base
 *     tags: [Knowledge Base]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - question
 *             properties:
 *               question:
 *                 type: string
 *               context:
 *                 type: string
 *     responses:
 *       200:
 *         description: AI answer with related documents
 */
router.post('/ai/ask', authenticate, knowledgeController.askAI);

export default router;
